package com.student.service;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import sun.misc.GC.LatencyRequest;

import com.student.DBConn;
import com.student.entities.SellerEntity;
import com.student.util.LonLat;

public class SellerService {
	public static List<SellerEntity> getAllSeller() throws SQLException {
		List<SellerEntity> sellerList = new ArrayList<SellerEntity>();
		Connection conn = DBConn.getConn();
		Statement stmt = conn.createStatement();
		String sql = "select * from shop_seller where seller_isVerify = 1";

		ResultSet rs = stmt.executeQuery(sql);

		while (rs.next()) {
			SellerEntity seller = new SellerEntity();
			seller.setId(rs.getInt("seller_id"));
			seller.setName(rs.getString("seller_name"));
			seller.setLogoURL(rs.getString("seller_logo"));
			seller.setAddress(rs.getString("seller_addr"));
			seller.setTel(rs.getString("seller_tel"));
			seller.setDeliverTime(rs.getString("seller_deliverTime"));
			seller.setDesc(rs.getString("seller_desc"));
			seller.setRank(rs.getDouble("seller_rank"));
			seller.setLat(rs.getDouble("seller_locLong"));
			seller.setLon(rs.getDouble("seller_locLat"));

			sellerList.add(seller);
		}
		rs.close();
		stmt.close();
		conn.close();
		return sellerList;
	}

	public static List<SellerEntity> getLoceller(String lon, String lat,
			String distance) throws SQLException {
		List<SellerEntity> sellerList = new ArrayList<SellerEntity>();
		Connection conn = DBConn.getConn();
		Statement stmt = conn.createStatement();
		String sql = "select * from shop_seller where seller_isVerify = 1";

		ResultSet rs = stmt.executeQuery(sql);

		while (rs.next()) {
			SellerEntity seller = new SellerEntity();
			seller.setId(rs.getInt("seller_id"));
			seller.setName(rs.getString("seller_name"));
			seller.setLogoURL(rs.getString("seller_logo"));
			seller.setAddress(rs.getString("seller_addr"));
			seller.setTel(rs.getString("seller_tel"));
			seller.setDeliverTime(rs.getString("seller_deliverTime"));
			seller.setDesc(rs.getString("seller_desc"));
			seller.setRank(rs.getDouble("seller_rank"));
			seller.setLat(rs.getDouble("seller_locLat"));
			seller.setLon(rs.getDouble("seller_locLong"));

			double dist = LonLat.distanceByLngLat(seller.getLon(), seller.getLat(),
					Double.valueOf(lon), Double.valueOf(lat));
			System.out.println(dist+"\t");
			if ( dist< Double
					.valueOf(distance)) {
				sellerList.add(seller);
			}

		}
		rs.close();
		stmt.close();
		conn.close();
		return sellerList;
	}

	public static void main(String args[]) throws SQLException {
		System.out.println(getLoceller("118.651244", "24.94608", "500.0"));
		System.out.println(LonLat.distanceByLngLat(118.651244, 24.94608, 118.651244, 24.94508));
	}
}
